
if (typeof global.Aconf === 'undefined') global.Aconf = { debug: 1 }

if (typeof global.Adebug === 'undefined') {
  global.Adebug = {
    init() {
      Adebug.colors = require('colors')
      Adebug.colors.setTheme({
        trace: 'green',
        silly: 'rainbow',
        input: 'grey',
        gray: 'gray',
        verbose: 'cyan',
        prompt: 'red',
        info: 'green',
        data: 'blue',
        help: 'cyan',
        warn: 'yellow',
        debug: 'magenta',
        error: 'red'
      })
    },
    color(e, args, colorFn, bg) {
      var er = e.stack.split('\n')
      er.splice(0, 2)
      var arr = []
      colorFn = colorFn || Adebug.colors.white
      for (var i = 0; i < args.length; i++) {
        if (typeof (args[i]) === 'object') arr.push(colorFn(JSON.stringify(args[i]).substr(0, 1000)))
        else arr.push(colorFn(args[i]))
      }
      arr.push(Adebug.colors.gray.underline(er.splice(0, e.n).join('\n')))
      return arr
    },
    _call(e, args, type, color) {
      try {
        args = this.color(e, args, Adebug.colors[color])
        args.unshift(`[${type}]: `)
        console.log.apply(console, args)
      } catch (error) {
        console.log(error)
      }
    },
    log(...args) {
      if (!Aconf.debug) return
      var e = { n: 1 }
      Error.captureStackTrace(e)
      Adebug._call(e, args, 'log', 'white')
    },
    warn(...args) {
      if (!Aconf.debug) return
      var e = { n: 1 }
      Error.captureStackTrace(e)
      Adebug._call(e, args, 'warn', 'warn')
    },
    info(...args) {
      if (!Aconf.debug) return
      var e = { n: 1 }
      Error.captureStackTrace(e)
      Adebug._call(e, args, 'info', 'info')
    },
    trace(...args) {
      if (!Aconf.debug) return
      var e = { n: 10 }
      Error.captureStackTrace(e)
      Adebug._call(e, args, 'trace', 'magenta')
    },
    error(...args) {
      if (!Aconf.debug) return
      var e = { n: 8 }
      Error.captureStackTrace(e)
      Adebug._call(e, args, 'error', 'error')
    }
  }
}

Adebug.init()
module.exports = Adebug
